//
//  PBMyOrderController.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/25.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBMyOrderController.h"
#import "TMOrderScrollView.h"
#import "TMMindTableView.h"
#import "TMMindTitleView.h"
#import "PBMyOrderModel.h"
#import "TMEmptyView.h"

@interface PBMyOrderController ()<UIScrollViewDelegate>
@property (nonatomic, weak) TMOrderScrollView *scrollView;
@property (nonatomic, weak) TMMindTitleView *titleView;
@property (nonatomic, weak) TMMindTableView *myPublicTableView;
@property (nonatomic, weak) TMMindTableView *myLikeTableView;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) TMEmptyView *emptyView;
@end

@implementation PBMyOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self requestMyPublic];
}

- (void)configureView {
    [self setupContentView];
    [self setupHeaderView];
}
- (void)requestMyPublic {
    self.page = 1;
    NSDictionary *param = @{@"pageSize":@"10",@"currentPage":@"1",@"status":@"1"};
    [TMNetwork requestGetWithParam:param url:@"/order/list" callback:^(TMBaseReqModel *model) {
        [self.myPublicTableView.mj_header endRefreshing];
        if (model.code == 200) {
            self.myPublicTableView.dataArray = [PBMyOrderModel objectArrayWithKeyValuesArray:model.data];
            if (self.myPublicTableView.dataArray.count < 10) {
                [self.myPublicTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.myPublicTableView.mj_footer endRefreshing];
            }
            if (self.myPublicTableView.dataArray.count == 0) {
                [self.view addSubview:self.emptyView];
            }else {
                [self.emptyView removeFromSuperview];
            }
            [self.myPublicTableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)loadMoreMyPublicData {
    self.page += 1;
    NSDictionary *param = @{@"pageSize":@"10",@"currentPage":[NSString stringWithFormat:@"%zd",self.page],@"status":@"1"};
    [TMNetwork requestGetWithParam:param url:@"/order/list" callback:^(TMBaseReqModel *model) {
        [self.myPublicTableView.mj_footer endRefreshing];
        if (model.code == 200) {
            NSArray *array = [PBMyOrderModel objectArrayWithKeyValuesArray:model.data];
            if (array.count < 10) {
                [self.myPublicTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.myPublicTableView.mj_footer endRefreshing];
            }
            [self.myPublicTableView.dataArray addObjectsFromArray:array];
            [self.myPublicTableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)requestMyLikeData {
    self.page = 1;
    NSDictionary *param = @{@"pageSize":@"10",@"currentPage":@"1",@"status":@"2"};
    [TMNetwork requestGetWithParam:param url:@"/order/list" callback:^(TMBaseReqModel *model) {
        [self.myLikeTableView.mj_header endRefreshing];
        if (model.code == 200) {
            self.myLikeTableView.dataArray = [PBMyOrderModel objectArrayWithKeyValuesArray:model.data];
            if (self.myLikeTableView.dataArray.count < 10) {
                [self.myLikeTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.myLikeTableView.mj_footer endRefreshing];
            }
            if (self.myLikeTableView.dataArray.count == 0) {
                [self.view addSubview:self.emptyView];
            }else {
                [self.emptyView removeFromSuperview];
            }
            [self.myLikeTableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)loadMoreMyLikeData {
    self.page += 1;
    NSDictionary *param = @{@"pageSize":@"10",@"currentPage":[NSString stringWithFormat:@"%zd",self.page],@"status":@"2"};
    [TMNetwork requestGetWithParam:param url:@"/order/list" callback:^(TMBaseReqModel *model) {
        [self.myLikeTableView.mj_header endRefreshing];
        if (model.code == 200) {
            NSArray *array = [PBMyOrderModel objectArrayWithKeyValuesArray:model.data];
            if (array.count < 10) {
                [self.myLikeTableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.myLikeTableView.mj_footer endRefreshing];
            }
            [self.myLikeTableView.dataArray addObjectsFromArray:array];
            [self.myLikeTableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

// 加载头部视图
- (void)setupHeaderView {
    TMMindTitleView *titleView = [[TMMindTitleView alloc] init];
    [self.view addSubview:titleView];
    self.titleView = titleView;
    titleView.backgroundColor = [UIColor whiteColor];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.equalTo(self.view.mas_top);
        make.height.mas_equalTo(OrderTitleHeight);
    }];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.mas_equalTo(self.view.top);
    }];
    
    __weak typeof (self) weakSelf = self;
    titleView.titles = @[@"待付款",@"已完成"];
    titleView.selectedIndex = 0;
    titleView.buttonSelected = ^(NSInteger index) {
        weakSelf.index = index;
        [weakSelf.scrollView setContentOffset:CGPointMake(kScreenWidth * index, 0) animated:YES];
        if (index == 0) {
            [weakSelf requestMyPublic];
        }else if (index == 1) {
            [weakSelf requestMyLikeData];
        }
    };
}

// 加载主要内容
- (void)setupContentView {
    TMOrderScrollView *scrollView = [[TMOrderScrollView alloc] init];
    scrollView.delaysContentTouches = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    scrollView.pagingEnabled = YES;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(kScreenWidth * 2, 0);
    
    TMMindTableView *myPublicTableView = [[TMMindTableView alloc] initWithFrame:CGRectMake(0, OrderTitleHeight, kScreenWidth, kScreenHeight-OrderTitleHeight-64) style:UITableViewStyleGrouped];
    myPublicTableView.type = @"待付款";
    myPublicTableView.jumpVC = self;
    myPublicTableView.separatorStyle      = UITableViewCellSeparatorStyleNone;
    myPublicTableView.showsVerticalScrollIndicator = NO;
    myPublicTableView.showsHorizontalScrollIndicator = NO;
    [TMRefreshHelper headerRefresh:myPublicTableView withTarget:self action:@selector(requestMyPublic)];
    [TMRefreshHelper footerRefresh:myPublicTableView withTarget:self action:@selector(loadMoreMyPublicData)];
    self.myPublicTableView                = myPublicTableView;
    [scrollView addSubview:myPublicTableView];
    
    TMMindTableView *myLikeTableView = [[TMMindTableView alloc] initWithFrame:CGRectMake(kScreenWidth,OrderTitleHeight, kScreenWidth, kScreenHeight-OrderTitleHeight-64) style:UITableViewStyleGrouped];
    myLikeTableView.type = @"已完成";
    myLikeTableView.jumpVC = self;
    myLikeTableView.separatorStyle      = UITableViewCellSeparatorStyleNone;
    myLikeTableView.showsVerticalScrollIndicator = NO;
    myLikeTableView.showsHorizontalScrollIndicator = NO;
    self.myLikeTableView                = myLikeTableView;
    [TMRefreshHelper headerRefresh:myLikeTableView withTarget:self action:@selector(requestMyLikeData)];
    [TMRefreshHelper footerRefresh:myLikeTableView withTarget:self action:@selector(loadMoreMyLikeData)];
    [scrollView addSubview:myLikeTableView];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        CGFloat contentOffsetX       = scrollView.contentOffset.x;
        NSInteger pageNum            = contentOffsetX / kScreenWidth;// + 0.5;
        self.titleView.selectedIndex = pageNum;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView || !scrollView.window) {
        return;
    }
    CGFloat offsetY      = scrollView.contentOffset.y;
    CGFloat originY      = 0;
    CGFloat otherOffsetY = 0;
    if (offsetY <= OrderHeadViewHeight) {
        originY              = -offsetY;
        if (offsetY < 0) {
            otherOffsetY         = 0;
        } else {
            otherOffsetY         = offsetY;
        }
    } else {
        originY              = -OrderHeadViewHeight;
        otherOffsetY         = OrderHeadViewHeight;
    }
    
    for ( int i = 0; i < self.titleView.titles.count; i++ ) {
        if (i != self.titleView.selectedIndex) {
            UITableView *contentView = self.scrollView.subviews[i];
            CGPoint offset = CGPointMake(0, otherOffsetY);
            if ([contentView isKindOfClass:[UITableView class]]) {
                if (contentView.contentOffset.y < OrderHeadViewHeight || offset.y < OrderHeadViewHeight) {
                    [contentView setContentOffset:offset animated:NO];
                    self.scrollView.offset = offset;
                }
            }
        }
    }
}

#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    self.title = @"我的订单";
}
- (void)backBarButtonItemClick:(UIBarButtonItem *)sener {
    self.titleView.selectedIndex = 0;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (TMEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [TMEmptyView TMEmptyView];
        _emptyView.frame = CGRectMake(0, 50, kScreenWidth, kScreenHeight-50);
    }
    return _emptyView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
